package com.hank.jms.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Enumeration;

public class Sender2 {
    public static String BROKER_URL="tcp://192.168.0.103:61616";
    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
        Connection connection = null;
        try {
            //通过工厂获取一个连接
            connection = connectionFactory.createConnection();
            connection.start();

            Enumeration enumeration=connection.getMetaData().getJMSXPropertyNames();
            while (enumeration.hasMoreElements()){
                System.out.println(enumeration.nextElement().toString());
            }
            //通过连接创建一个会话,并默认设开启事务,自动确认消息
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            //通过会话构建一个目的地
            Destination destination = session.createQueue("static-network-queue");
            //再通过session创建一个消息发送者，并设定发送目的地对象
            MessageProducer messageProducer = session.createProducer(destination);

            while (true){
                TextMessage message = session.createTextMessage("This is TextMessage >>");
                messageProducer.send(message);
                session.commit();
                Thread.sleep(300);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
